package com.tianyang.yukuaifu.business.main.presenter;

import com.alibaba.fastjson.JSONObject;
import com.tianyang.yukuaifu.business.main.contract.ServiceRegisterContract;
import com.tianyang.yukuaifu.business.main.model.MainModel;
import com.tianyang.yukuaifu.jump.eventbus.RefreshServiceManageEvent;
import com.tianyang.yukuaifu.utils.IntentTool;
import com.tianyang.yukuaifu.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：服务商信息
 * Created by xyj on 2024/10/20
 */
public class ServiceRegisterPresenter implements ServiceRegisterContract.Presenter {

    private ServiceRegisterContract.View mView;

    private CompositeSubscription mSubscription;

    public ServiceRegisterPresenter(ServiceRegisterContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void agentRegisterNew(String agentName, String phone, String password) {
        mView.showLoadingDialog(true,"正在提交中...");
        Subscription subscription = MainModel.getInstance().agentRegisterNew(agentName, phone, password)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<JSONObject>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showErrorText(e);
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onNext(JSONObject outputs) {
                        EventBus.getDefault().post(new RefreshServiceManageEvent());
                        ToastUtil.show("代理商注册成功！");
                        IntentTool.finishActivity(mView.getContext());
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
